﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Model;
using Besture.Utils;

namespace Besture.UI
{
    public partial class UserSet : BasePage
    {


        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            if (!IsPostBack)
            {

                if (Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    //编辑编号
                    hidEditID.Value = Request.QueryString["id"];
                    if (!Fun.IsNumber(hidEditID.Value))
                    {
                        RedirectErrorUrl("编号错误");
                    }
                    InitData(Convert.ToInt32(hidEditID.Value));
                }
            }
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            UserDac dac = new UserDac();
            UserModel model = dac.GetModel(id);
            txtPhone.Text = model.Phone != null ? model.Phone.ToString() : "";
            txtNickName.Text = model.NickName != null ? model.NickName.ToString() : "";
        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            UserDac dac = new UserDac();
            UserModel model = null;
            string editID = hidEditID.Value;

            if (string.IsNullOrEmpty(txtNickName.Text.Trim()))
            {
                Fun.Alert(this.Page,"姓名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtPhone.Text.Trim()))
            {
                Fun.Alert(this.Page,"手机号不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                Fun.Alert(this.Page,"密码不能为空");
                return;
            }

            DataTable dt = dac.GetDataTable(txtPhone.Text.Trim());

            //添加
            if (string.IsNullOrEmpty(editID))
            {
                model = new UserModel();
                model.Password = txtPassword.Text.Trim();
                model.Phone = txtPhone.Text.Trim();
                //如果存在返回1
                if (dt != null && dt.Rows.Count > 0)
                {
                    Fun.Alert(this.Page,"手机号码已存在");
                    return;
                }
                model.NickName = txtNickName.Text.Trim();
                model.IsDeleted = 0;
                model.CreateBy = LoginUserInfo.ID;
                model.CreateTime = DateTime.Now;
                model.UpdateBy = LoginUserInfo.ID;
                model.UpdateTime = DateTime.Now;

                model.Password = Fun.GetMD5(model.Password);
                dac.Add(model);
            }
            //编辑
            else
            {
                //如果存在返回1
                if (dt != null && dt.Rows.Count > 0 && dt.Rows[0]["ID"].ToString() != editID)
                {
                    Fun.Alert(this.Page, "手机号码已存在");
                    return;
                }
                model = dac.GetModel(Convert.ToInt32(editID));
                model.Password = txtPassword.Text.Trim();
                model.Phone = txtPhone.Text.Trim();
                model.NickName = txtNickName.Text.Trim();
                model.IsDeleted = 0;
                model.CreateBy = LoginUserInfo.ID;
                model.CreateTime = DateTime.Now;
                model.UpdateBy = LoginUserInfo.ID;
                model.UpdateTime = DateTime.Now;

                model.Password = Fun.GetMD5(model.Password);
                dac.Update(model);
            }
            Fun.Alert("保存成功", "UserList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("UserList.aspx");
        }

        #endregion


    }
}
